<template>
  <el-card class="w-full pt-[5px] pb0">
    <el-radio-group v-model="searchForm.validType" @change="handleSearch">
      <el-radio-button :label="`全部(${countMap.total})`" value="" />
      <el-radio-button :label="`有效(${countMap[0]})`" value="0" />
      <el-radio-button :label="`无效(${countMap[1]})`" value="1" />
    </el-radio-group>
    <el-form :model="searchForm" ref="searchFormRef" class="mt-[20px]" label-width="96px">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="单位名称" prop="name">
            <el-input v-model="searchForm.name" placeholder="单位名称" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="社会信用代码" prop="code">
            <el-input v-model="searchForm.code" placeholder="社会信用代码" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="单位性质" prop="buyerType">
            <CustomTreeSelect
              :dataList="buyerTypeList"
              placeholder="单位性质"
              v-model="searchForm.buyerType"
              :isFetch="false"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <div class="flex justify-end">
            <el-button type="primary" @click="searchFormOpen = !searchFormOpen"
              >更多
              <el-icon color="#fff">
                <CaretTop v-show="searchFormOpen" />
                <CaretBottom v-show="!searchFormOpen" />
              </el-icon>
            </el-button>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="20" v-show="searchFormOpen">
        <el-col :span="6">
          <el-form-item label="证书有效状态" prop="validTypeArr">
            <CustomTreeSelect
              :isFetch="false"
              :dataList="validTypeList"
              placeholder="证书有效状态"
              v-model="searchForm.validTypeArr"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="单位联系人" prop="contacts">
            <el-input v-model="searchForm.contacts" placeholder="单位联系人" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="单位电话" prop="telephone">
            <el-input v-model="searchForm.telephone" placeholder="单位电话" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="单位所在省份" prop="province">
            <CustomTreeSelect
              paramsName="甲方地区"
              placeholder="单位所在省份"
              v-model="searchForm.province"
              valueKey="codeColumn"
              labelKey="valueColumn"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
  <el-card class="w-full pt-[5px] mt-[10px]">
    <div class="mb-[20px]">
      <el-button type="primary" @click="handleAdd">
        <el-icon class="mr-[5px]"><Plus /></el-icon>
        新增
      </el-button>
      <el-button @click="handleDelSelect">
        <el-icon class="mr-[5px]"><DeleteFilled /></el-icon>
        删除
      </el-button>
    </div>
    <el-table
      :data="tableData"
      row-key="id"
      class="w-full"
      stripe
      v-loading="loading"
      ref="tableRef"
    >
      <el-table-column type="selection" width="40" />
      <el-table-column label="单位名称">
        <template #default="{ row }">
          <el-link type="primary" @click="handleShow(row.id)">{{ row.name }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="code" label="社会信用代码" width="200"/>
      <el-table-column prop="regFund" label="注册资金（万元）" width="150"/>
      <el-table-column label="单位性质" width="150">
        <template #default="{ row }">
          <CommonLabel :dataList="buyerTypeList" :value="row.buyerType" />
        </template>
      </el-table-column>
      <el-table-column label="证书有效状态" width="120">
        <template #default="{ row }">
          <CommonLabel :dataList="validTypeList" :value="row.validType" />
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150" align="center">
        <template #default="{ row }">
          <el-button type="primary" size="small" plain @click="handleEdit(row.id)">
            编辑
          </el-button>
          <el-button type="danger" size="small" plain @click="handleDel(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="mt-[20px] flex justify-end">
      <el-pagination
        v-bind="pagination"
        @update:current-page="currentChange"
        @update:page-size="sizeChange"
      />
    </div>
  </el-card>
  <EditModal ref="editModalRef" @save-success="handleSearch" />
  <ViewModal ref="viewModalRef" />
</template>

<script setup>
import useTable from "@/hooks/useTable";
import EditModal from "./components/EditModal.vue";
import ViewModal from "./components/ViewModal.vue";
import { getList, delRecords, getCount } from "@/api/project/yf/htgfdw";
import useCommonFetch from "@/hooks/useCommonFetch";
import CustomTreeSelect from "@/views/project/components/CustomTreeSelect.vue";
import CommonLabel from "@/views/project/components/CommonLabel.vue";
import { ElMessage } from "element-plus";
const searchFormOpen = ref(false);
const searchFormRef = ref();
const editModalRef = ref();
const viewModalRef = ref();
const tableRef = ref();
const buyerTypeList = useCommonFetch("甲方类别");
const validTypeList = useCommonFetch("证书有效状态");
const countMap = ref({
  total: 0,
  0: 0,
  1: 0,
});
const searchForm = ref({
  validType: "",
  name: "",
  code: "",
  buyerType: [],
  validTypeArr: [],
  contacts: "",
  telephone: "",
  province: [],
});
const { loading, pagination, sizeChange, currentChange, tableData, handleDelete, search } =
  useTable(
    (pagin) =>
      getList({
        pageNum: pagin.pageNum,
        pageSize: pagin.pageSize,
        ...searchForm.value,
      }),
    { getCountStatistics }
  );
getCountStatistics();
//点击新增
const handleAdd = () => {
  editModalRef.value.add();
};
//点击编辑
const handleEdit = (id) => {
  editModalRef.value.edit(id);
};
//点击名称查看
const handleShow = (id) => {
  viewModalRef.value.view(id);
};
//点击删除
const handleDel = (id) => {
  handleDelete(delRecords, id);
};
//批量删除
const handleDelSelect = () => {
  //获取当前选中行
  const selectRows = tableRef.value.getSelectionRows();
  if (!selectRows.length) {
    ElMessage({
      message: "请选择要删除的数据！",
      type: "warning",
    });
    return;
  }
  const ids = selectRows.map((item) => item.id);
  handleDelete(delRecords, ids.join(","));
};
//获取检索数量统计
async function getCountStatistics() {
  const params = {
    ...searchForm.value,
    validType: "",
  };
  try {
    const res = await getCount(params);
    countMap.value = {
      total: 0,
      0: 0,
      1: 0,
    };
    let totalCount = 0;
    res.data.forEach((item) => {
      countMap.value[item.validType] = item.count;
      totalCount += item.count;
    });
    countMap.value.total = totalCount;
  } catch (error) {
    console.log(error);
  }
}
//点击重置
const resetForm = () => {
  searchFormRef.value.resetFields();
  handleSearch();
};
//执行检索并获取数量统计
const handleSearch = () => {
  search();
  getCountStatistics();
};
</script>

<style scoped lang="scss"></style>
